package com.lagou.task18;

public class ThreadConmunicateTest implements Runnable{
    private int cnt = 1;
    @Override
    public void run() {
        while (true){

            synchronized (this) {
                //调用notify(),唤醒进程
                notify();
                if (cnt <= 100){
                    System.out.println("线程"  + Thread.currentThread().getName() + "中cnt= " + cnt);

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    cnt++;
                    //调用wait(),使当前进程进入阻塞状态.自动释放锁，wait必须在锁定的代码中使用
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        ThreadConmunicateTest tcct1 = new ThreadConmunicateTest();
        Thread t1 = new Thread(tcct1);
        t1.start();

//        ThreadConmunicateTest tcct2 = new ThreadConmunicateTest();
        Thread t2 = new Thread(tcct1);
        t2.start();

    }
}
